<template>
	<view class="vip-container">
		<!-- 当前状态 -->
		<view class="status-box">
			<text class="status-title">当前状态</text>
			<view class="status-content" v-if="userInfo.vip_type === 'free'">
				<text class="free-count">剩余{{ userInfo.free_chat_number }}条免费消息</text>
				<progress :percent="(userInfo.free_chat_number/50)*100" activeColor="#4cd964" />
			</view>
			<view class="vip-active" v-else>
				<text>会员有效期至：{{formatDate(userInfo.vip_expire_time)  }}</text>
			</view>
		</view>

		<!-- 套餐选择 -->
		<view class="package-grid" >
			<view class="package-card month">
				<view class="price-box">
					<text class="currency">¥</text>
					<text class="amount">9.9</text>
					<text class="unit">/月</text>
				</view>
				<text class="original-price">原价15元</text>
				<button class="buy-btn" @click="handlePurchase('month')">立即开通</button>
			</view>

			<view class="package-card year">
				<view class="price-box">
					<text class="currency">¥</text>
					<text class="amount">99</text>
					<text class="unit">/年</text>
				</view>
				<text class="original-price">原价180元</text>
				<button class="buy-btn" @click="handlePurchase('year')">限时优惠</button>
			</view>
		</view>

		<view class="invite-section"  >
			<view class="divider-text">或<text @click="handleInviteCode" style="color: black;">使用邀请码</text></view>
		</view>
		

		<!-- 特权说明 -->
		<view class="privilege-box">
			<text class="section-title">会员特权</text>
			<view class="privilege-list">
				<view class="privilege-item">
					<image src="/static/vip/icon-unlimited.png" class="privilege-icon" />
					<text class="privilege-text">无限量AI消息</text>
				</view>
				<view class="privilege-item">
					<image src="/static/vip/icon-model.png" class="privilege-icon" />
					<text class="privilege-text">专属高级模型</text>
				</view>
				<view class="privilege-item">
					<image src="/static/vip/icon-priority.png" class="privilege-icon" />
					<text class="privilege-text">优先响应支持</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		userApi,
		aiApi
	} from '@/utils/api.js'

	export default {
		data() {
			return {
				userInfo: {
					vip_type: 'free',
					free_chat_number: 50
				},
				vipStatus: 'free', // free | month | year
				freeMessageCount: 50
			}
		},
		onLoad() {
			userApi.getUserInfo().then(res => {
				this.userInfo = res.data;
				this.vipStatus = res.data.vip_type;
				this.freeMessageCount = res.data.free_chat_number
			})

			// 设置标题
			uni.setNavigationBarTitle({
				title: '会员中心'
			})
		},
		methods: {
			/**
			 * 格式化日期
			 */
			formatDate(date) {
				// 判空
				if (!date) {
					return ''
				}
				date = new Date(date)
				const year = date.getFullYear()
				const month = date.getMonth() + 1
				const day = date.getDate()
				return `${year}-${month}-${day}`
			},


			async handleInviteCode() {
				uni.showModal({
					title: '邀请码兑换',
					content: '请输入您的专属邀请码',
					editable: true,
					confirmText: '立即兑换',
					success: async (res) => {
						if (res.confirm && res.content) {
							uni.showLoading({
								title: '兑换中...'
							})
							try {
								const result = await aiApi.useInviteCode({
									invite_code: res.content.trim()
								})
								if (result.success) {
									uni.showToast({
										title: '会员开通成功!'
									})
									// 刷新用户信息
									const userRes = await userApi.getUserInfo()
									this.userInfo = userRes.data
								}else{
									uni.showToast({
										title: result.message,
										icon: 'none'
									})
								}
							} finally {
								uni.hideLoading()
							}
						}
					}
				})
			},
			async handlePurchase(type) {
				uni.showModal({
					title: '确认开通',
					content: `确定开通${type === 'month' ? '月度' : '年度'}会员？`,
					success: async (res) => {
						if (res.confirm) {
							// 调用支付接口
							const res = await aiApi.payVip({
								vip_type: type
							})
							if (res.success === true) {
								uni.requestPayment({
									provider: 'wxpay',
									timeStamp: res.data.timeStamp,
									nonceStr: res.data.nonceStr,
									package: res.data.package,
									signType: res.data.signType,
									paySign: res.data.paySign,
									success: (resPay) => {
										console.log(resPay);
									}
								})
							}
						}
					}
				})
			}
		}
	}
</script>

<style>
	.invite-section {
		margin: 40rpx 0;
		text-align: center;
	}

	.divider-text {
		color: #909399;
		margin: 20rpx 0;
		position: relative;
		font-size: 30rpx;
	}

	.divider-text::before,
	.divider-text::after {
		content: '';
		position: absolute;
		top: 50%;
		width: 30%;
		height: 1rpx;
		background: #eee;
	}

	.divider-text::before {
		left: 0;
	}

	.divider-text::after {
		right: 0;
	}

	 

	.vip-container {
		padding: 30rpx;
		background: #f8f9fa;
	}

	.status-box {
		background: #fff;
		padding: 30rpx;
		border-radius: 16rpx;
		margin-bottom: 30rpx;
	}

	.status-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}

	.free-count {
		display: block;
		margin: 20rpx 0;
		color: #666;
	}

	.package-grid {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 20rpx;
	}

	.package-card {
		background: #fff;
		padding: 30rpx;
		border-radius: 16rpx;
		text-align: center;
	}

	.month {
		border: 2rpx solid #4cd964;
	}

	.year {
		border: 2rpx solid #f0ad4e;
	}

	.price-box {
		display: flex;
		align-items: baseline;
		justify-content: center;
		margin: 20rpx 0;
	}

	.currency {
		font-size: 28rpx;
		color: #f0ad4e;
	}

	.amount {
		font-size: 48rpx;
		font-weight: bold;
		color: #f0ad4e;
		margin: 0 10rpx;
	}

	.unit {
		color: #999;
	}

	.original-price {
		color: #999;
		text-decoration: line-through;
	}

	.buy-btn {
		background: #4cd964;
		color: #fff;
		margin-top: 30rpx;
		border-radius: 50rpx;
	}

	.privilege-box {
		background: #fff;
		padding: 30rpx;
		border-radius: 16rpx;
		margin-top: 30rpx;
	}

	.privilege-item {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
	}

	.privilege-icon {
		width: 40rpx;
		height: 40rpx;
		margin-right: 20rpx;
	}

	.privilege-text {
		color: #666;
	}
</style>